﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Scriptor.Presentation.Entity;
using Scriptor.Presentation.Enumerator;

namespace Scriptor.Presentation.Templates
{
    public class TranslatorTemplate
    {
        List<string> gUsing { get; set; }
        public StringBuilder clausulaUsingFormatada { get; set; }

        List<string> gTranslator { get; set; }
        public StringBuilder translatorFormatado{ get; set; }

        List<string> gMethod { get; set; }
        public StringBuilder methodTranslatorFormatado { get; set; }

        public string strUsing(Enums.OrigemEnum source)
        {
            gUsing = new List<string>();
            gUsing.Add("using System;");
            gUsing.Add("using System.Collections.Generic;");

            if (source == Enums.OrigemEnum.BusinessEntity || source == Enums.OrigemEnum.DataContract)
            {
                gUsing.Add("using Sinacor.Infra.Common;");
            }
            else
                if (source == Enums.OrigemEnum.Communication)
                {
                    gUsing.Add("using System.Text;");
                    gUsing.Add("using Sinacor.Infra.UI.Common;");
                }
            
            clausulaUsingFormatada = new StringBuilder();

            foreach (string str in gUsing)
            {
                clausulaUsingFormatada.Append(str);
                clausulaUsingFormatada.Append(Environment.NewLine);
            }

            return clausulaUsingFormatada.ToString();
        }

        public string strTranslator(string pNameSpace, string pClassName)
        {
            gTranslator = new List<string>();

            gTranslator.Add("namespace " + pNameSpace);
            gTranslator.Add("{");
            gTranslator.Add("\tpublic class " + pClassName);
            gTranslator.Add("\t{");
            gTranslator.Add("@@class-body");
            gTranslator.Add("\t}");
            gTranslator.Add("}");

            translatorFormatado = new StringBuilder();

            foreach (string str in gTranslator)
            {
                translatorFormatado.Append(str);
                translatorFormatado.Append(Environment.NewLine);
            }

            return translatorFormatado.ToString();
        }

        public string strMethod(Method mtd, int BEOrDC, Param param)
        {
            gMethod = new List<string>();

            if (BEOrDC == 0)
            {
                //BusinessEntityToDataContract:

                if (!param.ParameterIsParameter)
                {
                    string unit = "DataContract.Response.{0}Response To{0}Response(BusinessEntity.{0} entity)";
                    string list = "List<DataContract.Response.{0}Response> To{0}Response(List<BusinessEntity.{0}> entities)";
                    string open = "\t\t{";
                    string close = "\t\t}";

                    gMethod.Add("\t\t" + "public " + string.Format(unit, mtd.ReturnName));
                    gMethod.Add(open);
                    gMethod.Add(close);
                    gMethod.Add("\t\t" + "public " + string.Format(list, mtd.ReturnName));
                    gMethod.Add(open);
                    gMethod.Add(close);
                }
            }
            else
                if (BEOrDC == 1)
                {
                    //DataContractToBusinessEntity:

                    string unit = "BusinessEntity.@@param{0} To{1}(DataContract.Request.{0}Request request)";
                    string list = "List<BusinessEntity.@@param{0}> To{1}(List<DataContract.Request.{0}Request> requests)";
                    string open = "\t\t{";
                    string close = "\t\t}";

                    string concat = string.Empty;

                    if (param.ParameterIsParameter)
                    {
                        concat = "Parameter.";
                    }

                    unit = unit.Replace("@@param", concat);
                    list = list.Replace("@@param", concat);

                    gMethod.Add("\t\t" + "public " + string.Format(unit, param.ParameterType, param.ParameterType));
                    gMethod.Add(open);
                    gMethod.Add(close);
                    gMethod.Add("\t\t" + "public " + string.Format(list, param.ParameterType, param.ParameterType));
                    gMethod.Add(open);
                    gMethod.Add(close);
                }
                else
                    if (BEOrDC == 2)
                    {
                        //DataContractToModel:

                        if (!param.ParameterIsParameter)
                        {
                            string unit = "Model.@@param{0} To{1}(DataContract.Response.{0}Response response)";
                            string list = "ModelCollection<Model.@@param{0}> To{1}(List<DataContract.Response.{0}Response> responses)";
                            string open = "\t\t{";
                            string close = "\t\t}";

                            string concat = string.Empty;

                            if (param.ParameterIsParameter)
                            {
                                concat = "Parameter.";
                            }

                            unit = unit.Replace("@@param", concat);
                            list = list.Replace("@@param", concat);

                            gMethod.Add("\t\t" + "public " + string.Format(unit, param.ParameterType, param.ParameterType));
                            gMethod.Add(open);
                            gMethod.Add(close);
                            gMethod.Add("\t\t" + "public " + string.Format(list, param.ParameterType, param.ParameterType));
                            gMethod.Add(open);
                            gMethod.Add(close);
                        }
                    }
                    else
                        if (BEOrDC == 3)
                        {
                            //ModelToDataContract:

                            string unit = "DataContract.Request.{0}Request To{1}Request(Model.@@param{0} model)";
                            //Paulo Silva - 14/05/2010
                            //Troque o final do nome do método List de Resposta Para Request
                            string list = "List<DataContract.Request.{0}Request> To{1}Request(ModelCollection<Model.@@param{0}> models)";
                            string open = "\t\t{";
                            string close = "\t\t}";

                            string concat = string.Empty;

                            if (param.ParameterIsParameter)
                            {
                                concat = "Parameter.";
                            }

                            unit = unit.Replace("@@param", concat);
                            list = list.Replace("@@param", concat);

                            gMethod.Add("\t\t" + "public " + string.Format(unit, param.ParameterType, param.ParameterType));
                            gMethod.Add(open);
                            gMethod.Add(close);
                            gMethod.Add("\t\t" + "public " + string.Format(list, param.ParameterType, param.ParameterType));
                            gMethod.Add(open);
                            gMethod.Add(close);

                        }
            
            methodTranslatorFormatado = new StringBuilder();

            foreach (string str in gMethod)
            {
                methodTranslatorFormatado.Append(str);
                methodTranslatorFormatado.Append(Environment.NewLine);
            }

            methodTranslatorFormatado.Append(Environment.NewLine);

            return methodTranslatorFormatado.ToString();
        }

        public string AppendMethod(string pClassScope, StringBuilder stb)
        {
            return pClassScope.ToString().Replace("@@class-body", stb.ToString());
        }
    }
}
